package com.sh.suhe.utildemos;

import android.content.ComponentName;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.jph.simple.PhotoMainActivity;
import com.ricky.d.welcompager2.ricky.RickyActivity;
import com.rippleview.RippleActivity;
import com.sh.suhe.utildemos.book.ui.BookActivity;
import com.sh.suhe.utildemos.canvas.CanvasActivity;
import com.sh.suhe.utildemos.deleteapp.DeleteAppActivity;
import com.sh.suhe.utildemos.dialogUtils.DialogMainActivity;
import com.sh.suhe.utildemos.getImageColor.ImageColorActivity;
import com.sh.suhe.utildemos.getViewPx.GetPxColorActivity;
import com.sh.suhe.utildemos.tell.PhoneListenerService;
import com.sh.suhe.utildemos.utils.DialogUtils;
import com.sh.suhe.utildemos.utils.OSUtils;
import com.sh.suhe.utildemos.views.activity.OutExcelActivity;
import com.sh.suhe.utildemos.views.activity.PhoneProActivity;
import com.sh.suhe.utildemos.views.activity.SocketActivity;
import com.sh.suhe.utildemos.views.activity.SocketScanActivity;
import com.sh.suhe.utildemos.views.activity.SurfaceAnimationActivity;
import com.sh.suhe.utildemos.views.activity.SysParamActivity;
import com.sh.suhe.utildemos.views.activity.cathtml.ArticleMainActivity;
import com.sh.suhe.utildemos.views.activity.draw.DrawMainActivity;
import com.sh.suhe.utildemos.views.activity.photo.PaPhotoActivity;
import com.yanzhenjie.permission.Action;
import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.runtime.Permission;
import com.yanzhenjie.permission.runtime.PermissionRequest;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.OnClick;

import static org.apache.xmlbeans.impl.schema.StscState.start;


public class MainActivity extends AppCompatActivity {
    private String TAG = MainActivity.class.getSimpleName();
    private String iconOne = "com.sh.suhe.utildemos.changeAfterOne";
    private String iconTwo = "com.sh.suhe.utildemos.changeAfterTwo";
    private String iconThree = "com.sh.suhe.utildemos.MainActivity";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

        Intent i = new Intent(this, PhoneListenerService.class);
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);//由于之前运行不能实现功能，经老师的修改加上一个任务标志
        this.startService(i);
    }


    @OnClick({R.id.main_title, R.id.main_dialog_but, R.id.main_photo_but,R.id.main_surface_but,
            R.id.main_excel_but,R.id.main_pa_but,R.id.main_sys_but,R.id.main_photo_pro_but,
            R.id.main_ripple_pro_but,R.id.main_ricky_but,R.id.main_socket_but,R.id.main_draw_but,
            R.id.main_pa_photo_but,R.id.main_book_but, R.id.main_canvas_but,R.id.main_pxcolor,
            R.id.main_image_color, R.id.main_delete_app, R.id.main_change_icon
    })
    public void onViewClicked(View view) {
        Intent intent = new Intent();
        switch (view.getId()) {
            case R.id.main_delete_app:
                intent.setClass(this, DeleteAppActivity.class);
                startActivity(intent);
                break;
            case R.id.main_image_color:
                intent.setClass(this, ImageColorActivity.class);
                startActivity(intent);
                break;
            case R.id.main_pxcolor:
                intent.setClass(this, GetPxColorActivity.class);
                startActivity(intent);
                break;
            case R.id.main_canvas_but:
                intent.setClass(this, CanvasActivity.class);
                startActivity(intent);
                break;

            case R.id.main_pa_photo_but:
                DialogUtils.createDialog(MainActivity.this, R.array.photo_type, new DialogInterface.OnClickListener(){

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        intent.putExtra("type", which + 1);
                        intent.setClass(MainActivity.this, PaPhotoActivity.class);
                        startActivity(intent);
                    }
                }).show();
                break;
            case R.id.main_title:
                break;
            case R.id.main_dialog_but:
                intent.setClass(this, DialogMainActivity.class);
                startActivity(intent);
                break;
            case R.id.main_photo_but:
                intent.setClass(this, PhotoMainActivity.class);
                startActivity(intent);
                break;
            case R.id.main_surface_but:
                intent.setClass(this, SurfaceAnimationActivity.class);
                startActivity(intent);
                break;
            case R.id.main_excel_but:
                intent.setClass(this, OutExcelActivity.class);
                startActivity(intent);
                break;
            case R.id.main_pa_but:
                intent.setClass(this, ArticleMainActivity.class);
                startActivity(intent);
                break;
            case R.id.main_sys_but:
                askPermission();

                break;
            case R.id.main_photo_pro_but:
                intent.setClass(this, PhoneProActivity.class);
                startActivity(intent);

                break;
            case R.id.main_ripple_pro_but:
                intent.setClass(this, RippleActivity.class);
                startActivity(intent);
                break;
            case R.id.main_ricky_but:
                intent.setClass(this, RickyActivity.class);
                startActivity(intent);
                break;
            case R.id.main_socket_but:
                intent.setClass(this, SocketScanActivity.class);
                startActivity(intent);
                break;
            case R.id.main_draw_but:
                intent.setClass(this, DrawMainActivity.class);
                startActivity(intent);
                break;
            case R.id.main_book_but:
                intent.setClass(this, BookActivity.class);
                startActivity(intent);
                break;
            case R.id.main_change_icon:
                String name = getComponentName().getClassName();
                Log.e(TAG, "old:" + name);
                if (iconOne.equals(name)) {
                    changeLauncher(iconTwo);
                }else if (iconTwo.equals(name)){
                    changeLauncher(iconThree);
                }else {
                    changeLauncher(iconOne);
                }

                break;
        }
    }
    public void askPermission() {
        AndPermission.with(this)
                .runtime()
                .permission(Permission.READ_PHONE_STATE)
                .onGranted(new Action<List<String>>() {
                    @Override
                    public void onAction(List<String> data) {
                        Intent intent = new Intent();
                        intent.setClass(MainActivity.this, SysParamActivity.class);
                        startActivity(intent);
                    }
                })
                .onDenied(new Action<List<String>>() {
                    @Override
                    public void onAction(List<String> data) {
                        Toast.makeText(MainActivity.this,"您拒绝了权限，有些功能会有影响！",Toast.LENGTH_SHORT).show(); //("您拒绝了权限，有些功能会有影响！")
                    }
                })
                .start();
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }
    private void changeLauncher(String name) {
        Log.e(TAG, name);

        PackageManager pm = getPackageManager();
        //隐藏之前显示的桌面组件
        pm.setComponentEnabledSetting(getComponentName(),

                PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);

//显示新的桌面组件

        pm.setComponentEnabledSetting(new ComponentName(MainActivity.this, name),

                PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);

    }
}
